<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="线索类型" prop="type">
                    <el-select v-model="queryParams.type" class="w-[280px]" clearable>
                        <el-option label="全部" value="" />
                        <el-option label="未转客户的线索" value="1" />
                        <el-option label="已转客户的线索" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="线索状态" prop="leadSource">
                    <el-select v-model="queryParams.followUpStatus" class="w-[280px]" clearable>
                        <el-option label="全部" value="" />
                        <el-option label="联系方式有效" value="1" />
                        <el-option label="未处理" value="2" />
                        <el-option label="联系方式无效" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="客户名称" prop="customerName">
                    <el-input class="w-[280px]" v-model="queryParams.customerName" />
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input class="w-[280px]" v-model="queryParams.phone" type="number" />
                </el-form-item>
                <el-form-item label="电话" prop="number">
                    <el-input class="w-[280px]" v-model="queryParams.number" type="number" />
                </el-form-item>

                <el-form-item label="业务人员" prop="businessPersonnelId">
                    <el-select v-model="queryParams.businessPersonnelId" class="w-[280px]" clearable>
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="协作人" prop="collaboratorId">
                    <el-select v-model="queryParams.collaboratorId" class="w-[280px]" clearable>
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建人" prop="userId">
                    <el-select v-model="queryParams.userId" class="w-[280px]" clearable>
                        <el-option label="张三" value="1" />
                        <el-option label="李四" value="2" />
                        <el-option label="王五" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="初次见面日期" prop="firstMeetingsTime">
                    <daterange-picker v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd" />
                </el-form-item>
                <el-form-item label="预计签单日期" prop="expectedOrderTime">
                    <daterange-picker v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd" />
                </el-form-item>
                <el-form-item label="最新跟进时间" prop="newFollowUpTime">
                    <daterange-picker v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd" />
                </el-form-item>

                <el-form-item label="创建时间" prop="createTime">
                    <daterange-picker v-model:startTime="queryParams.createTimeStart"
                        v-model:endTime="queryParams.createTimeEnd" />
                </el-form-item>
                <el-form-item label="意向业务" prop="intendedBusiness">
                    <el-input class="w-[280px]" v-model="queryParams.intendedBusiness" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <router-link style="margin-right: 10px" :to="{
                    path: getRoutePath('thread:add')
                }">
                    <el-button v-perms="['thread:add']" type="primary">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </router-link>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-download" />
                    </template>
                    导出
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-upload" />
                    </template>
                    导入
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-Avatar" />
                    </template>
                    批量修改业务员
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-User" />
                    </template>
                    批量修改协作人
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="local-icon-daiyunying" />
                    </template>
                    批量加入业务池
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column type="selection" width="55" />
                <el-table-column label="ID" prop="id" min-width="50" />
                <el-table-column label="客户名称" sortable prop="customerName" min-width="130">
                    <template #default="{ row }">
                        <router-link style="color: #c9161b" :to="{
                            path: getRoutePath('thread:check'),
                            query: {
                                id: row.id,
                                type: 'check'
                            }
                        }">
                            {{ row.customerName }}
                        </router-link>
                    </template>
                </el-table-column>
                <el-table-column label="部门名称" sortable prop="deptName" min-width="130" />
                <el-table-column label="姓名" sortable prop="name" min-width="130" />
                <el-table-column label="手机" sortable prop="phone" min-width="130" />
                <el-table-column label="电话" sortable prop="number" min-width="130" />
                <el-table-column label="线索来源" sortable prop="leadSource" min-width="130" />
                <el-table-column label="初次见面日期" prop="firstMeetingsTime" min-width="180" sortable />
                <el-table-column label="跟进状态" prop="followUpStatus" min-width="130" sortable />
                <el-table-column label="下次跟进时间" prop="followUpNextTime" min-width="180" sortable />
                <el-table-column label="预计销售金额" prop="estimatedSalesAmount" min-width="140" sortable />

                <el-table-column label="预计签单日期" prop="expectedOrderTime" min-width="180" sortable />
                <el-table-column label="意向业务" prop="intendedBusiness" min-width="130" sortable />
                <el-table-column label="业务人员" prop="businessPersonnelName" min-width="130" sortable />
                <el-table-column label="协作人" prop="collaboratorName" min-width="130" sortable />
                <el-table-column label="线索类型" prop="type" min-width="130" sortable />
                <el-table-column label="创建时间" prop="createTime" min-width="180" sortable />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['thread:edit']" type="primary" link>
                            <router-link :to="{
                                path: getRoutePath('thread:edit'),
                                query: {
                                    id: row.id
                                }
                            }">
                                编辑
                            </router-link>
                        </el-button>
                        <el-button v-perms="['thread:del']" type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="thread">
import { threadDelete, threadLists } from '@/api/thread'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'
const showEdit = ref(false)
const queryParams = reactive({
    customerName: '',
    deptName: '',
    name: '',
    followUpStatus: '',
    phone: '',
    number: '',
    leadSource: '',
    firstMeetingsTimeStart: '',
    firstMeetingsTimeEnd: '',
    estimatedSalesAmount: '',
    expectedOrderTimeStart: '',
    expectedOrderTimeEnd: '',
    intendedBusiness: '',
    businessPersonnelId: '',
    collaboratorId: '',
    type: '',
    newFollowUpTimeStart: '',
    newFollowUpTimeEnd: '',
    userId: '',
    createTimeStart: '',
    createTimeEnd: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: threadLists,
    params: queryParams
})

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await threadDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
